unit SingleInstanceWithParams; 
interface 
uses 
  Windows, SysUtils; 
const 
  PARAMS_SIZE = 1024; 
  MAPPING_NAME='{C363AE13-ADC5-4B61-A774-FB6DD5430E59}'; 
  NOTIFICATION_MSG_REG_STR = '{02926195-8CCC-4C8E-9506-FC80A7009962}'; 
  CRLF = #13#10; 
type 
  TInstanceDataHeader = record 
    hMainform: HWND; 
    dtStart: TDateTime; 
    // tudziez dowolne inne dane o stalej dlugosci 
  end; 
  PInstanceDataHeader = ^TInstanceDataHeader; 
var 
  hMapping: THandle; 
  pHeader: PInstanceDataHeader; 
  pParams: PChar; 
  WM_PARAM_NOTIFICATION: cardinal; 
function TryOpenMapping: boolean; 
procedure SetInitialInstanceData; 
procedure PassParamsAndNotify; 
function ReadParams: string; 
implementation 
uses Forms; 
function TryOpenMapping: boolean; 
begin 
  hMapping:=OpenFileMapping( 
    FILE_MAP_ALL_ACCESS, 
    false, 
    MAPPING_NAME 
    ); 
  result:=(hMapping<>0); 
end; // 
procedure SetInitialInstanceData; 
begin 
  hMapping:=CreateFileMapping( 
    $FFFFFFFF, 
    nil, 
    PAGE_READWRITE, 
    0, 
    SizeOf(TInstanceDataHeader)+PARAMS_SIZE, 
    MAPPING_NAME 
    ); 
  pHeader:=MapViewOfFile( 
    hMapping, 
    FILE_MAP_ALL_ACCESS, 
    0,0,0 
    ); 
  pParams:=pointer( 
    integer(pHeader)+ 
    SizeOf(TInstanceDataHeader) 
    ); 
WM_PARAM_NOTIFICATION:=RegisterWindowMessage(NOTIFICATION_MSG_REG_STR); 
  with pHeader^ do 
    begin 
      hMainform:=Application.Mainform.Handle; 
      dtStart:=Now; 
      // tudziez jakies inne dane 
    end; // 
  ZeroMemory(pParams, PARAMS_SIZE); 
end; // proc 
procedure PassParamsAndNotify; 
var 
  iCtr: integer; 
begin 
  pHeader:=MapViewOfFile( 
    hMapping, 
    FILE_MAP_ALL_ACCESS, 
    0,0,0); 
  pParams:=pointer( 
    integer(pHeader)+ 
    SizeOf(TInstanceDataHeader) 
    ); 
  ZeroMemory(pParams, PARAMS_SIZE); 
  // zapis parametrow 
  // ! nie jest sprawdzane ew. przepelnienie - ale to detal 
  for iCtr:=1 to ParamCount do 
    begin 
      StrCat( 
        pParams, 
        PChar(Paramstr(iCtr)+CRLF) 
        ); 
    end; // 
  with pHeader^ do 
    begin 
      SetForegroundWindow(hMainform); 
      SendMessage( 
        hMainform, 
        RegisterWindowMessage(NOTIFICATION_MSG_REG_STR), 
        0,0 
        ); 
    end; 
end; // 
function ReadParams: string; 
begin 
  pParams:=pointer( 
    integer(pHeader)+ 
    SizeOf(TInstanceDataHeader) 
    ); 
  result:=pParams; 
  UniqueString(result); 
end; // 
end. 